//
//  YCPhotoTransitionAnimator.swift
//  YCCamera
//
//  Created by Billy on 3/7/25.
//

import UIKit

class PhotoTransitionDelegate: NSObject, UIViewControllerTransitioningDelegate {
    
    var thumbnailFrame: CGRect = .zero
    
    // MARK: - Present 动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return YCPhotoTransitionAnimator(isPresenting: true, thumbnailFrame: thumbnailFrame)
    }
    
    // MARK: - Dismiss 动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return YCPhotoTransitionAnimator(isPresenting: false, thumbnailFrame: thumbnailFrame)
    }
}

class YCPhotoTransitionAnimator: NSObject, UIViewControllerAnimatedTransitioning {
    
    // MARK: - 属性
    let isPresenting: Bool  // 标记是 present 还是 dismiss
    let thumbnailFrame: CGRect  // 缩略图的初始位置
    
    init(isPresenting: Bool, thumbnailFrame: CGRect) {
        self.isPresenting = isPresenting
        self.thumbnailFrame = thumbnailFrame
        super.init()
    }
    
    // MARK: - 动画时长
    func transitionDuration(using transitionContext: (any UIViewControllerContextTransitioning)?) -> TimeInterval {
        return 0.3
    }
    
    // MARK: - 核心动画逻辑
    func animateTransition(using transitionContext: any UIViewControllerContextTransitioning) {
        let containerView = transitionContext.containerView
        
        if isPresenting {
            // present 动画：从缩略图放大到全屏
            guard let toVC = transitionContext.viewController(forKey: .to),
                  let toView = toVC.view else {
                transitionContext.completeTransition(false)
                return
            }
            
            // 创建临时动画视图
            let animateView = UIImageView(frame: thumbnailFrame)
//            animateView.image = (toVC as? YCPhotoDetailController)?.targetImage
            animateView.contentMode = .scaleAspectFill
            animateView.clipsToBounds = true
            containerView.addSubview(animateView)
            
            // 隐藏目标视图，仅显示动画视图
            toView.alpha = 0
            containerView.addSubview(toView)
            
            // 执行放大动画
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                animateView.frame = UIScreen.main.bounds
                toView.alpha = 1
            }) { _ in
                animateView.removeFromSuperview()
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            }
        } else {
            // Dismiss 动画：从全屏缩小回缩略图位置
            guard let fromVC = transitionContext.viewController(forKey: .from),
                  let fromView = fromVC.view else {
                transitionContext.completeTransition(false)
                return
            }
            
            // 创建临时动画视图
            let animateView = UIImageView(frame: UIScreen.main.bounds)
//            animateView.image = (fromVC as? PhotoDetailViewController)?.targetImage
            animateView.contentMode = .scaleAspectFill
            animateView.clipsToBounds = true
            containerView.addSubview(animateView)
            
            // 隐藏原视图
            fromView.alpha = 0
            
            // 执行缩小动画
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                animateView.frame = self.thumbnailFrame
                animateView.alpha = 0
            }) { _ in
                animateView.removeFromSuperview()
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            }
        }
    }
    
}
